package borrowingquantityDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bookDao.BookDbconnection;
import table.Book;
import table.Borrowingquantity;

public class BorrowingquantityImpl implements BorrowingquantityDao{
	String sql = null;
	int result = 0;
	@Override
	public List<Borrowingquantity> find(String x) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Borrowingquantity> borrowingquantity = new ArrayList<Borrowingquantity>();
		try {
			con = BookDbconnection.getConnection();
			sql = "SELECT borrowingQuantity_id,loanableQuantity,borrowedQuantity FROM borrowingquantity WHERE login_name=?";
			ps = con.prepareStatement(sql);
			ps.setString(1, x); 
			rs = ps.executeQuery();
			while (rs.next()) { 
				int borrowingQuantityId = rs.getInt(1);
				int loanableQuantity = rs.getInt(2);
				int borrowedQuantity = rs.getInt(3);
				Borrowingquantity bq = new Borrowingquantity(borrowingQuantityId, loanableQuantity, borrowedQuantity, x);
				borrowingquantity.add(bq);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BookDbconnection.close(rs, ps);
		}
		return borrowingquantity;
	}

	/**
	 *用于在图书借阅时 修改用户的可借数量
	 */
	@Override
	public void upate(String x,int y ,int z) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = BookDbconnection.getConnection();
			sql = "UPDATE borrowingquantity SET loanableQuantity=? ,borrowedQuantity=? WHERE login_name=?";
			ps = con.prepareStatement(sql);
			ps.setInt(1, y);
			ps.setInt(2, z);
			ps.setString(3, x);
			result = ps.executeUpdate();
			System.out.println("操作结果是" + result);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BookDbconnection.close(rs, ps);
		}
	}

	@Override
	public void save(Borrowingquantity x) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = BookDbconnection.getConnection();
			sql = "INSERT INTO borrowingquantity VALUE(null,?,?,?)";
			ps = con.prepareStatement(sql);
			ps.setString(3, x.getLogin_name());
			ps.setInt(2, x.getBorrowedQuantity());  
			ps.setInt(1, x.getLoanableQuantity());
			result = ps.executeUpdate(); 
			System.out.println("操作结果是" + result);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BookDbconnection.close(rs, ps);
		}
		
	}

	/**
	 *通过用户名 返回该用户数据数 判断其是否已经添加数据
	 */
	@Override
	public int findNumberOfDate(String x) {
		Connection con = null;
		PreparedStatement ps = null; 
		ResultSet rs = null;
		int i = 0; 
		try {
			con = BookDbconnection.getConnection();
			sql = "SELECT borrowingQuantity_id FROM borrowingquantity WHERE login_name=?";
			ps = con.prepareStatement(sql);
			ps.setString(1,x);
			rs = ps.executeQuery();			
			while(rs.next()) { 
				i++;
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}finally {
			BookDbconnection.close(rs, ps);
		}
		return i;
	}
}
